package com.lovo.emp.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.lovo.common.model.CutPageBean;
import com.lovo.common.model.EmpBean;
import com.lovo.emp.service.IEmpService;
import com.lovo.emp.service.impl.EmpServiceImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoCutPage;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.netCRM.component.LovoTitlePanel;
import com.lovo.netCRM.component.LovoTxt;


/**
 * 员工管理主面板
 * @author 白德柯
 *
 */
public class EmpPanel extends JPanel{

	/**员工表格组件*/
	private LovoTable empTable;
	/**窗体组件*/
	private JFrame jf;
	/**员工姓名文本框*/
	private LovoTxt nameTxt;
	/**文化程度文本框*/
	private LovoTxt educationTxt;
	/**所在部门文本框*/
	private LovoTxt deptTxt;
	/**工作职位文本框*/
	private LovoTxt workTxt;
	/**分页对象*/
	private LovoCutPage cutPage;

	private IEmpService empService = new EmpServiceImpl();

	public EmpPanel(JFrame jf){
		this.jf = jf;
		this.setLayout(null);
		this.init();
	}
	/**
	 * 初始化
	 *
	 */
	private void init() {
		new LovoTitleLabel("员 工 管 理",this);
		this.initTable();
		this.initButton();
		this.initFindPanel();
		this.initData();

	}
	/**
	 * 初始化数据
	 */
	public void initData(){
		this.updateTable(1);
	}
	/**
	 * 初始化按钮
	 *
	 */
	private void initButton() {
		LovoButton addButton = new LovoButton(50,460,"添加员工","image/add.png",this);
		
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new EmpAddDialog(jf, EmpPanel.this);
			}
		});
		
		LovoButton updateButton = new LovoButton(250,460,"修改员工","image/update.png",this);
		
		updateButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int id = empTable.getKeyByInt();
				if(id < 0){
					JOptionPane.showMessageDialog(EmpPanel.this,"请选择要修改的员工");
					return;
				}
				new EmpUpdateDialog(jf, id, EmpPanel.this);
			}
		});
		
		LovoButton delButton = new LovoButton(50,525,"删除员工","image/del.png",this);

		delButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int id = empTable.getKeyByInt();
				if(id < 0){
					JOptionPane.showMessageDialog(EmpPanel.this,"请选择要删除的员工");
					return;
				}
				int confirm = JOptionPane.showConfirmDialog(EmpPanel.this,"是否确认删除？");
				if(confirm == 0){
					empService.delete(id);
					initData();
				}
			}
		});

		LovoButton showButton = new LovoButton(250,525,"查看员工信息","image/find.png",this);
		
		showButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int id = empTable.getKeyByInt();
				if(id < 0){
					JOptionPane.showMessageDialog(EmpPanel.this,"请选择员工");
					return;
				}
				new EmpShowDialog(jf,id);
			}
		});

	}
	/**
	 * 初始化查询按钮
	 *
	 */
	private void initFindPanel(){

		LovoTitlePanel jp = new LovoTitlePanel("查询员工",400, 420, 320, 200,this);
		this.nameTxt = new LovoTxt("员工姓名",30,30,jp);
		this.educationTxt = new LovoTxt("文化程度",30,60,jp);
		this.deptTxt = new LovoTxt("所在部门",30,90,jp);
		this.workTxt = new LovoTxt("工作职位",30,120,jp);
		
		LovoButton findButton = new LovoButton("查找",180,150,jp);
		findButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				updateTable(1);
			}
		});
		findButton.setSize(60, 20);

		
	}

	
	//-------------------------------------------------
	/**
	 * 初始化表格
	 *
	 */
	private void initTable() {

		empTable = new LovoTable(this,
				new String[]{"姓名","性别","出生日期","文化程度","联系方式","所在部门","工作职位"},
				new String[]{"name","gender","born","education","phone","deptName","workName"},//员工实体属性名数组 new String[]{"name","sex"}
				"id");//主键属性名
		empTable.setSizeAndLocation(20, 90, 700, 300);

		// 分页
		cutPage = new LovoCutPage(20,400,this) {
			@Override
			public void clickEvent(int page) {
				updateTable(page);
			}
		};
		
	}
	/**
	 * 更新表格数据
	 */
	private void updateTable(int page){
		EmpBean empBean = new EmpBean();
		empBean.setName(nameTxt.getText());
		empBean.setEducation(educationTxt.getText());
		empBean.setDeptName(deptTxt.getText());
		empBean.setWorkName(workTxt.getText());
		CutPageBean cutPageBean = empService.findByItem(page,empBean);
		// 渲染表格数据
		empTable.updateLovoTable(cutPageBean.getList());
		// 当页码为第一页的时候设置分页组件显示的总页码
		if(page == 1){
			cutPage.setTotalPage(cutPageBean.getTotalPage());
		}
	}
	

}
